define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'form1/index',
        add_url: 'form1/add',
        edit_url: 'form1/edit',
        delete_url: 'form1/delete',
        export_url: 'form1/export',
        import_url: 'form1/import',
        modify_url: 'form1/modify',
        temperature_url: 'form1/temperature_index',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: [
                    'refresh'
                    , [{
                        text: '添加',
                        url: init.add_url,
                        method: 'open',
                        auth: 'add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-full="true"',
                    }]
                    , 'delete'
                    , [{
                        text: '批量导入',
                        url: init.import_url,
                        method: 'open',
                        auth: 'import',
                        class: 'layui-btn layui-btn-sm',
                        icon: 'fa fa-plus ',
                    }]
                ],
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID', width: 80},
                    {field: 'name', title: '姓名', width: 100},
                    {field: 'sex', search: 'select', selectList: ["男","女"], title: '性别', width: 80},
                    {field: 'id_card', title: '身份证', width: 180},
                    {field: 'mobile', title: '手机号', width: 150},
                    {field: 'arrival_time', title: '到沙时间', width: 160, search: 'range'},
                    {field: 'departure', title: '出发地', width: 160},
                    {field: 'check_time', title: '最近核酸检测', width: 160, search: 'range'},
                    {field: 'check_result', search: 'select', selectList: ["阴性","阳性"], title: '检测结果', width: 100, templet: function (d) {
                            if (d.check_result === 1) {
                                return '<div class="layui-table-cell" style="background-color: red;color: #fff;">阳性</div>';
                            }
                            return '<div class="layui-table-cell">阴性</div>';
                        }},
                    {field: 'community', title: '所属社区', selectList: getCommunityList, width: 150},
                    {field: 'status', title: '状态', selectList: ['未解除','已解除','已废弃'], width: 120, templet: function(d){
                            if (d.status === 1) {
                                return '<div class="layui-table-cell" style="background-color: green;color: #fff;">已解除</div>';
                            } else if (d.status === 2) {
                                return '<div class="layui-table-cell" style="background-color: #FFB800;color: #fff;">已废弃</div>';
                            } else {
                                return '<div class="layui-table-cell">未解除</div>';
                            }
                        }},
                    {field: 'count', title: '体温通知天数', width: 120, templet: function (d) {
                            return '<div class="layui-table-cell">' + d.count / 2 + '天</div>';
                        }
                    },
                    {field: 'sms_time', title: '最近通知时间', width: 160, templet: ea.table.date},
                    {field: 'create_time', title: '提交时间', width: 160, search: 'range'},
                    {
                        width: 180,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                text: '体温记录',
                                url: init.temperature_url,
                                method: 'open',
                                auth: 'temperature',
                                class: 'layui-btn layui-btn-xs layui-btn-normal',
                                extend: 'data-full="true"',
                                extra: 'name',
                            }, {
                                text: '编辑',
                                url: init.edit_url,
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-full="true"',
                                extra: 'name',
                            }, {
                                text: '删除',
                                title: '确定删除？',
                                url: init.delete_url,
                                method: 'request',
                                auth: 'delete',
                                class: 'layui-btn layui-btn-xs layui-btn-danger',
                                extra: 'name',
                            }]
                        ]
                        , fixed: 'right'
                    }
                ]],
            });

            ea.listen();
        },
        temperature_index: function () {
            ea.table.render({
                init: {
                    table_elem: '#currentTable',
                    table_render_id: 'currentTableRenderId',
                    index_url: 'form1/temperature_index?id=' + source_id,
                },
                toolbar: ['refresh',
                    [{
                        text: '添加',
                        url: 'form1/temperature_add?id=' + source_id,
                        method: 'open',
                        auth: 'temperature_add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                    }]
                ],
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID', width: 100, search: false},
                    {field: 'form1.name', title: '姓名', search: false},
                    {field: 'id_card', title: '身份证号'},
					{field: 'form1.mobile', title: '手机号'},
                    {field: 'temperature', title: '体温（℃）', search: false, templet: function (d) {
                            if (d.temperature > 37.3) {
                                return '<div class="layui-table-cell" style="background-color: red;color: #fff;">' + d.temperature + '</div>';
                            }
                            return '<div class="layui-table-cell">' + d.temperature + '</div>';
                        }},
                    {field: 'create_time', title: '上报时间', search: 'range'},
                ]],
            });

            ea.listen();
        },
        temperature_add: function () {
            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        import: function () {
            var msg;
            layui.upload.render({
                elem: '.upload'
                ,url: ea.url(init.import_url)
                ,accept: 'file'
                ,exts: 'xlsx'
                ,before: function(obj){
                    msg = layer.msg('上传中...', {
                        icon: 16
                        ,shade: 0.01
                        ,time: 200000
                    });
                }
                ,done: function(res, index, upload){
                    layer.close(msg);
                    if (res.code) {
                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                            let index = parent.layer.getFrameIndex(window.name);
                            parent.$('[data-table-refresh]').trigger("click");
                            parent.layer.close(index);
                        });
                        return true;
                    }
                    layer.msg(res.msg, {icon: 2});
                }
                ,progress: function(n){
                    percent = n + '%';
                    $('.layui-layer-padding').html('<i class="layui-layer-ico layui-layer-ico16"></i>'+  '上传中...' + percent);
                }
            });

            ea.listen();
        },
    };
    return Controller;
});